﻿namespace Innovative.ProXr
{
	/// <summary>
	/// </summary>
	public class DwordArrayResponseConverter : IResponseConverter<int[]>
	{
		int[] IResponseConverter<int[]>.Convert(RelayByte[] response)
		{
			int[] returnValue = new int[response.Length / 2];

			if (response.Length > 0 && response.Length % 2 == 0)
			{
				int j = 0;

				for (int i = 0; i < response.Length; i += 2)
				{
					returnValue[j] = (int)(response[i] + (256 * response[i + 1]));
					j++;
				}
			}
			else
			{
				throw new InvalidResponseException("The DwordArrayResponseConverter converter expects one or more bytes in the response and also requires and even number of bytes in the response.");
			}

			return returnValue;
		}
	}
}
